package com.atguigu.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * @author lvbingbing
 * @date 2020-08-03 23:10
 */
@Slf4j
public class HBaseUtil {

    /**
     * 禁止使用默认构造函数，以确保其不稳定性。
     */
    private HBaseUtil() {

    }

    /**
     * 验证表是否创建成功
     *
     * @param connection 当前连接
     * @param admin      管理员
     * @param student    TableName对象
     * @throws IOException
     */
    public static void validateTable(Connection connection, Admin admin, TableName student) throws IOException {
        if (admin.tableExists(student)) {
            log.info("表创建成功，表名：{}", student.getNameAsString());
            Table studentTable = connection.getTable(student);
            HTableDescriptor tableDescriptor = studentTable.getTableDescriptor();
            HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
            for (HColumnDescriptor columnDescriptor : columnFamilies) {
                log.info("列族名：{}", columnDescriptor.getNameAsString());
            }
        }
    }

    /**
     * 校验名称空间是否创建成功
     *
     * @param admin         管理员
     * @param nameSpaceName 名称空间名称
     * @throws IOException
     */
    public static void checkCreateNamespace(Admin admin, String nameSpaceName) throws IOException {
        NamespaceDescriptor weibo = admin.getNamespaceDescriptor(nameSpaceName);
        if (weibo != null) {
            String name = weibo.getName();
            log.info("名称空间创建成功！name：{}", name);
            Map<String, String> configuration = weibo.getConfiguration();
            Set<Map.Entry<String, String>> entries = configuration.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                String key = entry.getKey();
                String value = entry.getValue();
                log.info("相关配置：key：{}, value：{}", key, value);
            }
        }
    }
}
